
public class Test {
    //按顺序连接两链表
    public SingleLinkedList.ListNode mergeTwoLists
            (SingleLinkedList.ListNode list1, SingleLinkedList.ListNode list2) {
        SingleLinkedList.ListNode newHead = new SingleLinkedList.ListNode(-1);
        SingleLinkedList.ListNode tmp = newHead;
        while(list1 != null && list2 != null){
            if(list1.val < list2.val){
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            }else{
                tmp.next = list2;
                list2 = list2.next;
                tmp = tmp.next;
            }
        }
        if(list1 == null){
            tmp.next = list2;
        }
        else{
            tmp.next = list1;
        }
        return newHead.next;
    }
    //求两个链表Y性相交结点
    public SingleLinkedList.ListNode getIntersectionNode
    (SingleLinkedList.ListNode headA, SingleLinkedList.ListNode headB) {
        int szA = 0;
        int szB = 0;
        SingleLinkedList.ListNode curA = headA;
        SingleLinkedList.ListNode curB = headB;
        //求A链表长度
        while(curA != null){
            curA = curA.next;
            szA++;
        }
        //求B链表长度
        while(curB != null){
            curB = curB.next;
            szB++;
        }
        curA = headA;
        curB = headB;
        if(szA > szB){
            //求差值，因为是Y性，所有相同结点后面的长度是一样的，所以求差值，让长的走完这段差值，两个再同时走
            int diff = szA - szB;
            while(diff != 0){
                curA = curA.next;
                diff--;
            }
        }
        else{
            int diff = szB - szA;
            while(diff != 0){
                curB = curB.next;
                diff--;
            }
        }
        while(curA != curB){
/*            if(curA == null){
                return null;
            }*/ //判不判断无所谓，因为最后还是会返回null
            curA = curA.next;
            curB = curB.next;
        }
        return curA;
    }
    public static void main(String[] args){
       SingleLinkedList singleLinkedList = new SingleLinkedList();
        singleLinkedList.addLast(1);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(3);
        singleLinkedList.addLast(2);
        singleLinkedList.addLast(1);
        singleLinkedList.display();
        System.out.println("===================");
        boolean ret = singleLinkedList.chkPalindrome();
        System.out.println(ret);

    }
}
